package com.genexususa.eo.google.data;

import com.google.api.ads.adwords.axis.v201406.cm.*;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.StringReader;

/**
 * Created with IntelliJ IDEA.
 * User: Emiliano Martinez
 * Date: 11/6/12
 * Time: 2:16 PM
 * To change this template use File | Settings | File Templates.
 */
public class Util {


    public Document loadXMLFromString(String xml) throws Exception{
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        factory.setNamespaceAware(true); // never forget this!
        InputSource is = new InputSource(new StringReader(xml));
        return builder.parse(is);
    }

    public static DayOfWeek dayOfWeekFromString(String dayOfWeekString) {
        if (dayOfWeekString.equalsIgnoreCase("SU")) {
            return DayOfWeek.SUNDAY;
        } else if (dayOfWeekString.equalsIgnoreCase("MO")) {
            return DayOfWeek.MONDAY;
        } else if (dayOfWeekString.equalsIgnoreCase("TU")) {
            return DayOfWeek.TUESDAY;
        } else if (dayOfWeekString.equalsIgnoreCase("WE")) {
            return DayOfWeek.WEDNESDAY;
        } else if (dayOfWeekString.equalsIgnoreCase("TH")) {
            return DayOfWeek.THURSDAY;
        } else if (dayOfWeekString.equalsIgnoreCase("FR")) {
            return DayOfWeek.FRIDAY;
        } else if (dayOfWeekString.equalsIgnoreCase("SA")) {
            return DayOfWeek.SATURDAY;
        }
        // Default value (This should never happen)
        return DayOfWeek.MONDAY;
    }

    public static String dayOfWeekToString(DayOfWeek dayOfWeek) {
        if (dayOfWeek.equals(DayOfWeek.SUNDAY)) {
            return "SU";
        } else if (dayOfWeek.equals(DayOfWeek.MONDAY)) {
            return "MO";
        } else if (dayOfWeek.equals(DayOfWeek.TUESDAY)) {
            return "TU";
        } else if (dayOfWeek.equals(DayOfWeek.WEDNESDAY)) {
            return "WE";
        } else if (dayOfWeek.equals(DayOfWeek.THURSDAY)) {
            return "TH";
        } else if (dayOfWeek.equals(DayOfWeek.FRIDAY)) {
            return "FR";
        } else if (dayOfWeek.equals(DayOfWeek.SATURDAY)) {
            return "SA";
        }
        // Default value (This should never happen)
        return "MO";
    }

    public static MinuteOfHour minuteOfHourFromString(String minuteOfHourString) {
        if (minuteOfHourString.equalsIgnoreCase("0")) {
            return MinuteOfHour.ZERO;
        } else if (minuteOfHourString.equalsIgnoreCase("15")) {
            return MinuteOfHour.FIFTEEN;
        } else if (minuteOfHourString.equalsIgnoreCase("30")) {
            return MinuteOfHour.THIRTY;
        } else if (minuteOfHourString.equalsIgnoreCase("45")) {
            return MinuteOfHour.FORTY_FIVE;
        }
        // Default value (This should never happen)
        return MinuteOfHour.ZERO;
    }

    public static int minuteOfHourToInteger(MinuteOfHour minuteOfHour) {
        if (minuteOfHour.equals(MinuteOfHour.ZERO)) {
            return 0;
        } else if (minuteOfHour.equals(MinuteOfHour.FIFTEEN)) {
            return 15;
        } else if (minuteOfHour.equals(MinuteOfHour.THIRTY)) {
            return 30;
        } else if (minuteOfHour.equals(MinuteOfHour.FORTY_FIVE)) {
            return 45;
        }
        // Default value (This should never happen)
        return 0;
    }

    public static LocationTargetingStatus targetingStatusFromString(String targetingStatusString) {
        if (targetingStatusString.equalsIgnoreCase("A")) {
            return LocationTargetingStatus.ACTIVE;
        } else if (targetingStatusString.equalsIgnoreCase("O")) {
            return LocationTargetingStatus.OBSOLETE;
        } else if (targetingStatusString.equalsIgnoreCase("P")) {
            return LocationTargetingStatus.PHASING_OUT;
        }
        // Default value (This should never happen)
        return LocationTargetingStatus.ACTIVE;
    }

    public static String targetingStatusToString(LocationTargetingStatus targetingStatus) {
        if (targetingStatus.equals(LocationTargetingStatus.ACTIVE)) {
            return "A";
        } else if (targetingStatus.equals(LocationTargetingStatus.OBSOLETE)) {
            return "O";
        } else if (targetingStatus.equals(LocationTargetingStatus.PHASING_OUT)) {
            return "P";
        }
        // Default value (This should never happen)
        return "A";
    }

    public static CriterionUse criterionUseFromString(String criterionUseString) {
        if (criterionUseString.equalsIgnoreCase("B")) {
            return CriterionUse.BIDDABLE;
        } else if (criterionUseString.equalsIgnoreCase("N")) {
            return CriterionUse.NEGATIVE;
        }
        // Default value (This should never happen)
        return CriterionUse.BIDDABLE;
    }

    public static String criterionUseToString(CriterionUse criterionUse) {
        if (criterionUse.equals(CriterionUse.BIDDABLE)) {
            return "B";
        } else if (criterionUse.equals(CriterionUse.NEGATIVE)) {
            return "N";
        }
        // Default value (This should never happen)
        return "B";
    }

    public static MarkupLanguageType markupLanguageFromString(String markupLanguageString) {
        if (markupLanguageString.equalsIgnoreCase("H")) {
            return MarkupLanguageType.HTML;
        } else if (markupLanguageString.equalsIgnoreCase("C")) {
            return MarkupLanguageType.CHTML;
        } else if (markupLanguageString.equalsIgnoreCase("X")) {
            return MarkupLanguageType.XHTML;
        } else if (markupLanguageString.equalsIgnoreCase("W")) {
            return MarkupLanguageType.WML;
        }
        // Default value (This should never happen)
        return MarkupLanguageType.HTML;
    }

    public static String markupLanguageToString(MarkupLanguageType markupLanguageType) {
        if (markupLanguageType.equals(MarkupLanguageType.HTML)) {
            return "H";
        } else if (markupLanguageType.equals(MarkupLanguageType.CHTML)) {
            return "C";
        } else if (markupLanguageType.equals(MarkupLanguageType.XHTML)) {
            return "X";
        } else if (markupLanguageType.equals(MarkupLanguageType.WML)) {
            return "W";
        }
        // Default value (This should never happen)
        return "H";
    }
}
